<?php

namespace app\console\command;

use app\common\model\Vod;
use HuaweiCloud\SDK\Vod\V1\Model\ShowAssetMetaResponse;
use HuaweiVod\HuaweiVod;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class UploadVod extends Command
{
    protected function configure()
    {
        $this->setName('upload_vod')
            ->setDescription('upload video files to huawei vod');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("execute upload vod");
        try {

            $video = Db::name('video')->where(['id' => 105])->find();
            $videoList = Db::name('videolist')->where('id', $video['pid'])->find();
            $video['videourl'] = fullMediaUrl($video['videourl']);
//            $output->info(json_encode($video));
//            $response = new ShowAssetMetaResponse();
//            $response = HuaweiVod::make()->assetInfo('2fed7312d84ea04fb460d2f1c814fbbb');
//
//            if ($response) {
//
//            }
//            $output->info(json_decode($response));
//            $output->info(json_encode($response));
//            Log::info($response . '');
            $this->uploadVideo(104);
        } catch (\Exception $exception) {
            Log::error($exception->getMessage());
            Log::error($exception->getTraceAsString());
//            $output->error($exception->getMessage());
        }
    }

    protected function uploadVideo($videoId)
    {
        $video = Db::name('video')->where(['id' => $videoId])->find();
        $videoList = Db::name('videolist')->where('id', $video['pid'])->find();
        $video['videourl'] = fullMediaUrl($video['videourl']);
        $title = $videoList['name'] . '-' . $video['name'];
        $result = HuaweiVod::make()->uploadByUrl($video['videourl'], $title);
        if ($result) {
            $result = json_decode($result, true);
            Log::info('result:' . json_encode($result));
            Vod::addVod($videoId, $videoList['id'], $title, 'MP4', $result['upload_assets'][0]['asset_id'], $result['upload_assets'][0]['url']);
        }
    }
}
